"use strict";

const dev_middleware = require("webpack-dev-middleware");
const express = require("express");
const hot_middleware = require("webpack-hot-middleware");
const webpack = require("webpack");

const app = express();
const config = require("./webpack.config.js");
const compiler = webpack(config);
const hotMiddleware = require('webpack-hot-middleware')(compiler, {
    log: () => {},
    heartbeat: 2000
});

app.use(dev_middleware(compiler, {
    logLevel: "warn",
    noInfo: true,
    hot: true,
    reload: true,
    publicPath: config.output.publicPath
}));

app.use(hotMiddleware);

compiler.hooks.compilation.tap('html-webpack-plugin-after-emit', () => {
    hotMiddleware.publish({
      action: 'reload'
    });
});

// Serve files in 8080.
app.listen(8080, function () {
    console.log("Listening: http://localhost:8080");
});
